<?php
/*
	本插件：$this->model('diaoca')->方法名();
	其他地方调用：model('jy_yxw/diaoca')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class diaoca extends \plugin{
	public function down_csv($params=array()){
		$list=pdo_getall('jy_yxw_diaoca_tiku',$params);
		foreach($list as &$rv){
			if($rv['type']==4){
				$rv['type']='单选题';
			}
			if($rv['type']==5){
				$rv['type']='多选题';
			}
			if($rv['type']==20){
				$rv['type']='判断题';
			}
			$value=json_decode($rv['xuanxiang'],true);
			$rv['value']='';
			foreach($value as $vv){
				if($rv['value']){
					$rv['value'] .='|'.'['.$vv['score'].'分] '.$vv['title'];
				}else{
					$rv['value'] .='['.$vv['score'].'分] '.$vv['title'];
				}
			}
		}
		$header=array(
				'title'=>'题目标题',
				'type'=>'题目类型',
				'score'=>'题目分数',
				'value'=>'选项内容',
		);
		cfc('tools')->export2excel($header,$list,'[题库列表]');
	}
	public function gettikus($item){
		$params=array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'tiku_type'=>$item['type']);
		if($item['keywords']){
			$params['title like']='%'.trim($item['keywords']).'%';
			$return['params']['keywords']=$item['keywords'];
		}
		$filedata=array('id','title','desc','score','type','daan','xuanxiang','images');
		$tikus=pdo_getall('jy_yxw_diaoca_tiku',$params,$filedata);
		foreach($tikus as &$iv){
			$iv['xuanxiang']=json_decode($iv['xuanxiang'],true);
			$iv['images']=json_decode($iv['images'],true);
		}
		return $tikus;
	}
	public function add_canyu($params=array())
	{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'datiid'=>$params['datiid'],
			'type'=>$params['type'],
			'storeid'=>$params['storeid'],
			'score'=>0,
			'allscore'=>$params['allscore'],
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'content'=>ijson_encode($params['content']),
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yxw_diaoca_log',$insert);
		$id=pdo_insertid();
		return $id;
	}
	public function up_canyu($params=array()){
		$content=$params['content'];
		$update=array(
			'content'=>ijson_encode($content),
		);
		pdo_update('jy_yxw_diaoca_log',$update,array('id'=>$params['id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function getscore($content){
		$score=0;
		$allscore=0;
		foreach($content as &$item){
			$allscore +=$item['score'];
			$item['getscore']=$item['xuanxiang'][$item['data']]['score'];
			$score +=$item['getscore'];
		}
		$res=array('score'=>$score,'allscore'=>$allscore,'content'=>$content);
		return $res;
	}
}
?>